﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using Projet_IMA.Base;


namespace Projet_IMA.Unsafe
{
    public unsafe class UnsafeBitmap 
    {
        public Bitmap Bitmap { get; set;}

        private int Width = 1920;
        private int Height = 1080;

        private BitmapData BitmapData = null;
        private Byte* BitmapByte = null;

        public UnsafeBitmap()
        {
            Bitmap = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
            this.Reset();
        }

        public void StartDraw()
        {
            this.Reset();

            BitmapData = Bitmap.LockBits(new Rectangle(Point.Empty, Bitmap.Size), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            BitmapByte = (Byte*)BitmapData.Scan0.ToPointer();
        }

        public void SetPixel(int x, int y, UnsafeColor* c)
        {
            int i = y * BitmapData.Stride + x * 3 ;

            BitmapByte[i] = c->B;
            BitmapByte[i + 1] = c->G;
            BitmapByte[i + 2] = c->R;
        }

        public void EndDraw()
        {
            Bitmap.UnlockBits(BitmapData);
            BitmapData = null;
            BitmapByte = null;
        }

        private void Reset()
        {
            Graphics g = Graphics.FromImage(Bitmap);
            g.Clear(Color.White);
        }

    }
}
